#!/bin/bash

#待编译文件名及可执行文件名
#服务器端
SERVERFILE='server/server.c'
#目标目录
BINARYDIR='/usr/local/bin/'
SERVER=$BINARYDIR'ftpd'
#客户端
CLIENTFILE='client/client.c'
CLIENT=$BINARYDIR'ftpc'
#配置文件位置
LOCALCONF='server/ftpd.conf'
LOCALUSERFILE='server/users'
CONFPATH='/etc/ftpd.d'
CONFFILE='ftpd.conf'
#控制文件
LOCALCTRLFILE='ftpdctl'
CTRLFILE=$BINARYDIR
#编译器名称
MAKETOOL='gcc'

USERFILE=$(cat $LOCALCONF | grep user_file | cut -d '=' -f2)

if [ ! -w $AIMPATH ];then
	echo 'Please ensure that you hava write access to'$AIMPATH 
fi

if [ ! -d $CONFPATH ];then
	mkdir -m 0755 -p $CONFPATH
else
	chmod -R 0755 $CONFPATH
fi

echo 'Making binary files'
$MAKETOOL -o $SERVER $SERVERFILE -lpthread
$MAKETOOL -o $CLIENT $CLIENTFILE -lpthread
echo 'Done...'
echo 'Copying config file to'$CONFPATH
cp $LOCALCONF $CONFPATH
echo 'Done...'
echo 'Copying user file to'$USERFILE
cp $LOCALUSERFILE $USERFILE
echo 'Done...'
echo 'Copying control file to'$CTRLFILE
cp $LOCALCTRLFILE $CTRLFILE
echo 'Done...'
echo "Don't forget to edit config file $CONFPATH/$CONFFILE"

chmod 0755 $SERVER
chmod 0755 $CLIENT
